package com.app.micaihu.view.main.dialog

import android.content.DialogInterface
import android.os.Bundle
import com.app.base.BaseDialogFragment
import com.app.micaihu.R
import com.app.micaihu.bean.game.GameDownloadEntity
import com.app.micaihu.configure.Config
import com.app.micaihu.custom.components.downandupload.DownloadUtils
import com.app.micaihu.view.utils.loadRadius
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.dialog_uninstall_game.*
import java.io.File

class UnInstallGameDialogFragment : BaseDialogFragment() {
    companion object {
        @JvmStatic
        fun newInstance(gameItem: GameDownloadEntity?): UnInstallGameDialogFragment {
            val unInstallFileDialogFragment = UnInstallGameDialogFragment()
            val bundle = Bundle()
            bundle.putSerializable(Config.Extra.item, gameItem)
            unInstallFileDialogFragment.arguments = bundle
            return unInstallFileDialogFragment
        }
    }

    var gameItem: GameDownloadEntity? = null
    override fun getBundle(bundle: Bundle) {
        super.getBundle(bundle)
        gameItem = bundle.getSerializable(Config.Extra.item) as GameDownloadEntity
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_uninstall_game
    }


    override fun setData() {
        gameItem?.let {
            ivGameIcon.loadRadius(it.logo, 5)
            tvGameName.text = it.name
            tvGameSize.text = it.appSize
        }
    }

    override fun addListeners() {
        ivClose.setOnClickListener {
            dismiss()
        }
        btnInstall.setOnClickListener {
            gameItem?.let {
                val unInstallFile = File(DownloadUtils.DOWNLOAD_FILEDIR, it.iPath + ".apk")
                if (unInstallFile.exists() && unInstallFile.isFile) {
                    AppUtils.installApp(unInstallFile)
                    dismiss()
                } else {
                    ToastUtils.showShort("文件不存在")
                }
            }

        }
    }

    override fun canceledOnTouchOutside(): Boolean {
        return false
    }

    override fun cancelable(): Boolean {
        return false
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
    }
}